package com.wcy.entity.pageVo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.annotation.pageVo.PageColumn;
import com.wcy.annotation.pageVo.PageName;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.List;

@Data
public class PageData<T> {
    private String name;
    private Long total;
    private List<T> data;
    private List<Column> column;
    private ApiDescriptor api;
    private String deleteAlias;//删除按钮别名
    private String updateAlias;//编辑按钮别名

    public PageData(Class<T> pageClass,Page<T> page, ApiDescriptor api,ColumnConstructor additionalColumnConstuctor){
//        System.out.println(page);
        this.total = page.getTotal();
        this.data = page.getRecords();
        if(pageClass.isAnnotationPresent(PageName.class)){
            PageName pageName = (PageName) pageClass.getAnnotation(PageName.class);
            this.name = (pageName).name();
            this.deleteAlias = pageName.deleteAlias();
            this.updateAlias = pageName.updateAlias();
        }else {
            this.name = pageClass.getSimpleName();
        }
        ColumnConstructor columnConstuctor = new ColumnConstructor();
        for (Field field : pageClass.getDeclaredFields()) {
//            System.out.println(field.getName());
            if (field.isAnnotationPresent(PageColumn.class)) {
                PageColumn pageColumn = field.getAnnotation(PageColumn.class);
                if(pageColumn.display())
                    columnConstuctor.add(pageColumn);
            }else{
                String colName = field.getName();
                columnConstuctor.add(colName,colName,"text",true);
            }
        }
//        System.out.println("result:"+columnConstuctor.getResult());
        this.api = api;
        columnConstuctor.merge(additionalColumnConstuctor);
        column = columnConstuctor.getResult();
    }


    public PageData<T> setAlias(String deleteAlias,String updateAlias){
        this.deleteAlias = deleteAlias;
        this.updateAlias = updateAlias;
        return this;
    }

    //根据字段名在编辑层面修改列
    public PageData<T> overrideColumn(String prop,Column col){
        for (Column column1 : column) {
            System.out.println(prop+"?>"+column1.getProp());
            if(column1.getProp() != null&&column1.getProp().equals(prop)){
                System.out.println(prop+"->"+col);
                column1.overrideColumn(col);
            }
        }
        return this;
    }
}
